package ch.zbw.pra.sysventory.ui.gui.panels.overviewandsearchpanel.subpanels.detailPanel;

import java.awt.Component;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

public class DetailTableModel implements TableModel, TableCellRenderer, TableCellEditor {
	private List<TableModelListener> listenerTableModel;
	private List<CellEditorListener> listenerTableEditor;
	private List<List<String>> rawData;
	private List<String> header;
	private int rowCount;

	public DetailTableModel() {
		this.listenerTableModel = new ArrayList<TableModelListener>();
		this.listenerTableEditor = new ArrayList<CellEditorListener>();
		this.rawData = new ArrayList<List<String>>();
		this.header = new ArrayList<String>();
		this.rowCount = 0;
	}

	public DetailTableModel(List<List<String>> rawData, List<String> header) {
		super();
		if (rawData == null || header == null) {
			throw new IllegalArgumentException("Is nott allowed to set a null rowData or header");
		}
		this.rawData = rawData;
		this.header = header;
		this.rowCount = rawData.size();
	}

	public void addCellEditorListener(CellEditorListener listener) {
		if (listener != null) {
			this.listenerTableEditor.add(listener);
		}
	}

	public void cancelCellEditing() {
		// TODO Auto-generated method stub

	}

	public Object getCellEditorValue() {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean isCellEditable(EventObject arg0) {
		// TODO Auto-generated method stub
		return true;
	}

	public void removeCellEditorListener(CellEditorListener listener) {
		if (listener != null) {
			this.listenerTableEditor.remove(listener);
		}

	}

	public boolean shouldSelectCell(EventObject arg0) {
		return true;
	}

	public boolean stopCellEditing() {
		return true;
	}

	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
		Component comp = null;

		@SuppressWarnings("unchecked")
		List<String> contentList = (List<String>) value;
		if (contentList.size() > 1) {
			comp = new JComboBox(contentList.toArray());
		}
		return comp;
	}

	public Component getTableCellRendererComponent(JTable table, Object aValue, boolean isSelected, boolean hasFocus, int row, int column) {
		JComponent comp = null;
		comp = new JLabel(this.rawData.get(row).get(column));
		return comp;
	}

	public void addTableModelListener(TableModelListener listener) {
		if (canAccessEmptyListener(listener)) {
			// crasht aus irgend einem Grund,deswegen ist dieser Punkt
			// deaktiviert...
			// this.listenerTableModel.add(listener);

		}
	}

	private boolean canAccessEmptyListener(TableModelListener listener) {
		return listener != null;
	}

	public Class<?> getColumnClass(int columnIndex) {
		return new ArrayList<String>().getClass();
	}

	public int getColumnCount() {
		return this.header.size();
	}

	public String getColumnName(int columnIndex) {
		return this.header.get(columnIndex);
	}

	public int getRowCount() {
		return this.rowCount;
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		if (canAccesList(rowIndex, columnIndex)) {
			return this.rawData.get(rowIndex).get(columnIndex);
		}
		return null;
	}

	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	public void removeTableModelListener(TableModelListener listener) {
		if (listener != null) {
			this.listenerTableModel.remove(listener);
		}

	}

	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		if (rowIndex >= this.rawData.size()) {
			this.rawData.add(rowIndex, new ArrayList<String>());
			rowIndex++;
		}
		this.rawData.get(rowIndex).add(columnIndex, (String) aValue);

	}

	private boolean canAccesList(int rowIndex, int columnIndex) {
		return rowIndex < this.rawData.size() && columnIndex < this.rawData.get(rowIndex).size();

	}

	public void setElementList(List<List<String>> rawData) {
		this.rawData = rawData;
		this.rowCount = this.rawData.size();
	}

	public void setHeaderList(List<String> header) {
		this.header = header;
	}

}
